
function registerInfCheck(){
	$(".tips").hide();  // 隐藏所有错误信息

	$("#studentNu").blur(function(){
		checkStudentNu();
	});

	$("#studentPw").blur(function(){
		checkPassword();
	});
	
	$("#name_r").blur(function(){
		checkName();
	});
	
	$("#phoneNu").blur(function(){
		checkPhoneNu();
	});
	loadDepartment();
	loadDormBdName("#bd");
}

function register(){
	if(checkStudentNu() &&
	checkPassword() &&
	checkName() &&
	checkDepartment() &&
	checkMajorAndGrade() &&
	checkDorm() &&
	checkPhoneNu()){
		submit_inf();
	}
}

function submit_inf(){
	$.post("setRegister",
		{studentNu: $("#studentNu").val(),
		password: $("#studentPw").val(),
		name: $("#name_r").val(),
		sex: $("#sex").val(),
		department: $("#department").val(),
		major: $("#major").val(),
		grade: $("#grade").val(),
		bdName: $("#bd").val(),
		dormNu: $("#dormNu").val(),
		contactInf: $("#phoneNu").val()
		},
		function(json){
			if(json.options){
				alert("注册成功");
				window.location.href = "/studormcms/";
			}else{
				alert("注册失败");
			}
		});
}

function checkStudentNu(){
	var studentNu = $("#studentNu").val();

	$("#nuInvalid").show();
	if(isNaN(studentNu)){
		$("#nuInvalid").text("学号只能是数字");
		return false;
	}
	if(studentNu.length != 8){
		$("#nuInvalid").text("学号必须为8位数");
		return false;
	}

	$("#nuInvalid").hide(); // 如果没有错误，隐藏
	return true;
}

function checkPassword(){
	var studentPw = $("#studentPw").val().toString();

	if(studentPw.length < 6){
		$("#pwInvalid").text("密码长度必须大于等于6位。");
		$("#pwInvalid").show();
		return false;
	}
	$("#pwInvalid").hide();
	return true;
}

function checkName(){
	var name_r = $("#name_r").val();
	
	if(name_r.length < 2){
		$("#nameInvalid").show();
		return false;
	}else{
		$("#nameInvalid").hide();
		return true;
	}
}

function checkDepartment(){
	if($("#department").val() == "请选择"){
		$("#deparInvalid").show();
		return false;
	}else{
		$("#deparInvalid").hide();
		return true;
	}
}

function checkMajorAndGrade(){
	if($("#major").val() == "请选择"){
		$("#majorGradeInvalid").text("请选择专业");
		$("#majorGradeInvalid").show();
		return false;
	}else{
		if($("#grade").val() == "请选择"){
			$("#majorGradeInvalid").text("请选择年级");
			$("#majorGradeInvalid").show();
			return false;
		}else{
			$("#majorGradeInvalid").hide();
			return true;
		}
	}
}

function checkDorm(){
	if($("#bd").val() == "请选择"){
		$("#dormInvalid").text("请选择宿舍楼");
		$("#dormInvalid").show();
		return false;
	}else{
		if($("#dormNu").val() == "请选择"){
			$("#dormInvalid").text("请选择宿舍号");
			$("#dormInvalid").show();
			return false;
		}else{
			$("#dormInvalid").hide();
			return true;
		}
	}
}

function checkPhoneNu(){
	var phoneNu = $("#phoneNu").val();
	if(phoneNu.length != 11){
		$("#phoneInvalid").show();
		return false;
	}else{
		$("#phoneInvalid").hide();
		return true;
	}
}

function loadDepartment(){
	var oOption = document.createElement("option");
	oOption.text = "请选择";
	$("#department")[0].options.add(oOption);
	$.post("getDepartment", function(json){
		$.each(json.options, function(i, item){
			var oOption = document.createElement("option");
			oOption.text = item;
			oOption.value = item;
			$("#department")[0].options.add(oOption);
		});
	});
}

function loadMajorByDepartment(){
	$("#grade").empty();
	$("#major").empty();
	var oOption = document.createElement("option");
	oOption.text = "请选择";
	$("#major")[0].options.add(oOption);
	$.post("getMajorByDepartment",
			{department: $("#department").val()}, 
			function(json){
				$.each(json.options, function(i, item){
					var oOption = document.createElement("option");
					oOption.text = item;
					oOption.value = item;
					$("#major")[0].options.add(oOption);
				});
			});
}

function loadGrade(){
	$("#grade").empty();
	var oOption = document.createElement("option");
	oOption.text = "请选择";
	$("#grade")[0].options.add(oOption);
	$.post("getGrade",
			{department: $("#department").val(),
		major: $("#major").val()}, 
		function(json){
			$.each(json.options, function(i, item){
				var oOption = document.createElement("option");
				oOption.text = item;
				oOption.value = item;
				$("#grade")[0].options.add(oOption);
			});
		});
}

function loadDormBdName(obj){
	$(obj).empty();
	$(obj).append($("<option value='请选择'>请选择</option>"));
	$.post("getDormBdName", function(json){
		$.each(json.options, function(i, item){
			var oOption = document.createElement("option");
			oOption.text = item;
			oOption.value = item;
			$(obj)[0].options.add(oOption);//添加option方法1
			//http://bbs.csdn.net/topics/200070161 (2楼)
		});
	});
}

function loadDormNu(obj, from, showAll) {
	$(obj).empty();//http://blog.csdn.net/Feiin/article/details/3896937
	$(obj).append($("<option value='请选择'>请选择</option>"));
	if(showAll){
		$(obj).append($("<option value='全部'>全部</option>"));
	}
	$.post("getDormNu", {dornBdName: $(from).val()}, function(json){
		$.each(json.options, function(i, item){
			$(obj).append($("<option value='"+item+"'>"+item+"</option>"));//添加option方法2
			//http://www.xiaoxiaozi.com/2010/09/29/1935/
		});
	});
}